package com.mycompany.myapp.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.mycompany.myapp.domain.Shu;
import com.mycompany.myapp.service.ShuService;
import com.mycompany.myapp.web.rest.errors.BadRequestAlertException;
import com.mycompany.myapp.web.rest.util.HeaderUtil;
import com.mycompany.myapp.web.rest.util.PaginationUtil;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Shu.
 */
@RestController
@RequestMapping("/api")
public class ShuResource {

    private final Logger log = LoggerFactory.getLogger(ShuResource.class);

    private static final String ENTITY_NAME = "shu";

    private final ShuService shuService;

    public ShuResource(ShuService shuService) {
        this.shuService = shuService;
    }

    /**
     * POST  /shus : Create a new shu.
     *
     * @param shu the shu to create
     * @return the ResponseEntity with status 201 (Created) and with body the new shu, or with status 400 (Bad Request) if the shu has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/shus")
    @Timed
    public ResponseEntity<Shu> createShu(@RequestBody Shu shu) throws URISyntaxException {
        log.debug("REST request to save Shu : {}", shu);
        if (shu.getId() != null) {
            throw new BadRequestAlertException("A new shu cannot already have an ID", ENTITY_NAME, "idexists");
        }
        Shu result = shuService.save(shu);
        return ResponseEntity.created(new URI("/api/shus/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /shus : Updates an existing shu.
     *
     * @param shu the shu to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated shu,
     * or with status 400 (Bad Request) if the shu is not valid,
     * or with status 500 (Internal Server Error) if the shu couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/shus")
    @Timed
    public ResponseEntity<Shu> updateShu(@RequestBody Shu shu) throws URISyntaxException {
        log.debug("REST request to update Shu : {}", shu);
        if (shu.getId() == null) {
            return createShu(shu);
        }
        Shu result = shuService.save(shu);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, shu.getId().toString()))
            .body(result);
    }

    /**
     * GET  /shus : get all the shus.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of shus in body
     */
    @GetMapping("/shus")
    @Timed
    public ResponseEntity<List<Shu>> getAllShus(Pageable pageable) {
        log.debug("REST request to get a page of Shus");
        Page<Shu> page = shuService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/shus");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /shus/:id : get the "id" shu.
     *
     * @param id the id of the shu to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the shu, or with status 404 (Not Found)
     */
    @GetMapping("/shus/{id}")
    @Timed
    public ResponseEntity<Shu> getShu(@PathVariable Long id) {
        log.debug("REST request to get Shu : {}", id);
        Shu shu = shuService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(shu));
    }

    /**
     * DELETE  /shus/:id : delete the "id" shu.
     *
     * @param id the id of the shu to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/shus/{id}")
    @Timed
    public ResponseEntity<Void> deleteShu(@PathVariable Long id) {
        log.debug("REST request to delete Shu : {}", id);
        shuService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
